//
//  SRFindPwdViewController.m
//  SportRing
//
//  Created by Apple on 14/10/24.
//  Copyright (c) 2014年 HotOne. All rights reserved.
//

#import "SRFindPwdViewController.h"
#import "SRFindPwdView.h"
#import "SRAccountNetService.h"

@interface SRFindPwdViewController () <SRFindPwdViewDelegate>
{
    NSInteger _i;
    NSTimer *_timer;
}
@property (nonatomic, weak) IBOutlet SRFindPwdView * findPwdView;

@end

@implementation SRFindPwdViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.hidesBackButton = YES;
    
}

- (void)viewWillAppear:(BOOL)animated
{
    if (_timer) {
        [_timer invalidate];
    }
    self.edgesForExtendedLayout=UIRectEdgeNone;
    self.navigationController.navigationBarHidden = YES;
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardwillChange:) name:UIKeyboardWillChangeFrameNotification object:nil];
}

- (void)keyboardWillShow:(NSNotification *)notification
{
    //得到键盘高度
    NSDictionary *userInfo = [notification userInfo];
    NSValue* aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
    CGRect keyboardRect = [aValue CGRectValue];
    
    
    [UIView animateWithDuration:0.3 animations:^{
        _findPwdView.center = CGPointMake(_findPwdView.center.x, self.view.center.y - keyboardRect.size.height);
    }];
    
}

- (void)keyboardwillChange:(NSNotification *)notification
{
    //得到键盘高度
    NSDictionary *userInfo = [notification userInfo];
    NSValue* aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
    CGRect keyboardRect = [aValue CGRectValue];
    
    [UIView animateWithDuration:0.3 animations:^{
        _findPwdView.center = CGPointMake(_findPwdView.center.x, self.view.center.y - keyboardRect.size.height);
    }];
}

- (void)keyboardWillHide:(NSNotification *)notification
{
    _findPwdView.center = self.view.center;
    
}

- (void)viewWillDisappear:(BOOL)animated
{
    self.navigationController.navigationBarHidden = NO;
    [self.view endEditing:YES];
}

#pragma mark - SRFindPwdViewDelegate

- (void)doSendAuthCodeAction:(NSString *)mobile
{
    [SVProgressHUD show];
    [[SRAccountNetService getAuthCodeWithPhoneNumber:mobile] subscribeNext:^(id x) {
        [SVProgressHUD showSuccessWithStatus:@"请稍等, 短信马上就到."];
        
        [_findPwdView.sendAuthBtn setTitle:[NSString stringWithFormat:@"120%@",NSLocalizedString(@"秒后重发", nil)] forState:UIControlStateNormal];
        [_findPwdView.sendAuthBtn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
        _findPwdView.sendAuthBtn.enabled = NO;
        _i = 120;
        _timer = [NSTimer timerWithTimeInterval:1 target:self selector:@selector(sendBtnClick) userInfo:nil repeats:YES];
        [[NSRunLoop currentRunLoop] addTimer:_timer forMode:NSRunLoopCommonModes];
    } error:^(NSError *error) {
        [SVProgressHUD showErrorWithStatus:@"获取验证码失败"];
    }];
}


- (void)sendBtnClick
{
    _i--;
    [_findPwdView.sendAuthBtn setTitle:[NSString stringWithFormat:@"%ld%@",_i,NSLocalizedString(@"秒后重发", nil)] forState:UIControlStateNormal];
    _findPwdView.sendAuthBtn.titleLabel.text = [NSString stringWithFormat:@"%ld%@",_i,NSLocalizedString(@"秒后重发", nil)];
    if (_i == 0) {
        [_timer invalidate];
        [_findPwdView.sendAuthBtn setTitle:[NSString stringWithFormat:@"%@",NSLocalizedString(@"发送验证码", nil)] forState:UIControlStateNormal];
        _findPwdView.sendAuthBtn.enabled = YES;
        [_findPwdView.sendAuthBtn setTitleColor:[UIColor colorWithRed:144.0/255.0 green:87.0/255.0 blue:192.0/255.0 alpha:1] forState:UIControlStateNormal];
    }
}

- (void)doModifyNewPasswordAction:(NSString *)newPwd authCode:(NSString *)authCode phoneNumber:(NSString *)phoneNumber
{
    @weakify(self);
    [SVProgressHUD show];
//    [[SRAccountNetService modifyPasswordWithSms:authCode
//                                         newPwd:newPwd] subscribeNext:^(id x) {
//        [SVProgressHUD showSuccessWithStatus:@"修改成功"];
//        [self_weak_.navigationController popViewControllerAnimated:YES];
//    } error:^(NSError *error) {
//        [SVProgressHUD showErrorWithStatus:@"修改密码失败"];
//    }];
    
    [[SRAccountNetService modifyPasswordWithSms:authCode
                                        newPwd:newPwd
                                   phoneNumber:phoneNumber] subscribeNext:^(id x) {
        [SVProgressHUD showSuccessWithStatus:@"修改成功"];
        [self_weak_.navigationController popViewControllerAnimated:YES];
    } error:^(NSError *error) {
         [SVProgressHUD showErrorWithStatus:@"修改密码失败"];

    }];
}
- (IBAction)backLigin:(UIButton *)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

@end
